package com.learn.observer.normal;

import com.learn.observer.WeatherType;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>项目名称: design-pattern </p>
 * <p>文件名称: Weather </p>
 * <p>描述: TODO </p>
 *
 * @author zhousiqi
 * @version v0.0.1
 * @since 2018/10/18 16:39
 */
@Slf4j
public class Weather {

    private WeatherType currentWeather;
    private String detail;

    private List<WeatherObserver> observers;

    public Weather(String detail) {
        this.detail = detail;
        currentWeather = WeatherType.SUNNY;
        observers = new ArrayList<>();
    }

    public void addObserver(WeatherObserver observer){
        observers.add(observer);
    }

    public void removeObserver(WeatherObserver observer){
        observers.remove(observer);
    }

    public void timePass(){
        WeatherType[] enumValues = WeatherType.values();
        currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
        log.info("The weather of {} changed to {}.", detail, currentWeather);
        notifyObservers();
    }

    private void notifyObservers() {
        observers.forEach(x -> x.update(currentWeather,detail));
    }


}
